<?php

/**
 * message actions.
 *
 * @package    s-test
 * @subpackage message
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class messageActions extends sfActions
{

  public function executeNew(sfWebRequest $request)
  {
    $this->forward404Unless($topic_id = $request->getParameter(('id')));
    $this->forward404Unless($this->ktd_topic = Doctrine::getTable('KTDTopic')->find(array('id' => $request->getParameter('id'))), sprintf("Le sujet où vous souhaitez répondre n'existe pas ou plus."));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasPermission('post_message'), 'Vous ne pouvez pas poster de message');
        
    $this->form = new KTDMessageForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $this->forward404Unless($this->ktd_topic = Doctrine::getTable('KTDTopic')->find(array('id' => $request->getParameter('topic_id'))), sprintf("Le sujet où vous souhaitez répondre n'existe pas ou plus."));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasPermission('post_message'), 'Vous ne pouvez pas poster de message');


    $ktd_message = new KTDMessage();

    $ktd_message->setTopicId($this->ktd_topic->getId());
    $ktd_message->setPlayerId($this->getUser()->getGuardUser()->getId());

    $this->form = new KTDMessageForm($ktd_message);
    $this->processForm($request, $this->form, true);
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($ktd_message = Doctrine::getTable('KTDMessage')->find(array($request->getParameter('id'))), sprintf('Le message que vous souhaitez éditer n\'existe pas ou plus.'));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasRelPermission('edit_message', $ktd_message->getKTDPlayer()), sprintf('Vous ne pouvez pas éditer ce message.'));
    $this->form = new KTDMessageForm($ktd_message);
    $this->ktd_message = $ktd_message;
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($ktd_message = Doctrine::getTable('KTDMessage')->find(array($request->getParameter('id'))), sprintf('Ce message n\'existe plus (%s).', $request->getParameter('id')));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasRelPermission('edit_message', $ktd_message->getKTDPlayer()), sprintf('Vous ne pouvez pas éditer ce message.'));

    $this->form = new KTDMessageForm($ktd_message);
    $this->ktd_message = $ktd_message;
    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->forward404Unless($ktd_message = Doctrine::getTable('KTDMessage')->find(array($request->getParameter('id'))), sprintf('Object ktd_message does not exist (%s).', $request->getParameter('id')));
    $ktd_char = $ktd_message->getKTDPlayer();
    if($ktd_message->isFirstMessage())
    {
        $this->forward404Unless($ktd_topic = Doctrine::getTable('KTDTopic')->find(array($request->getParameter('id'))), sprintf('Object ktd_topic does not exist (%s).', $request->getParameter('id')));
        $this->forward404Unless($this->getUser()->getGuardUser()->hasRelPermission('delete_topic'), "Vous n'avez pas la permission de supprimer ce sujet");
        $ktd_forum = $ktd_topic->getKTDForum();
        KTDEventManager::getSingleton()->handleTopic(KTDTopicHandler::$onDelete, array("topic"=>$ktd_topic));
        $ktd_topic->delete();
        $this->redirect('show_forum', $ktd_forum);
    }
    else
    {
        
        $this->forward404Unless($this->getUser()->getGuardUser()->hasRelPermission('delete_message', $ktd_message->getKTDPlayer()), sprintf('Vous ne pouvez pas effacer ce message.'));
        $ktd_topic = $ktd_message->getKTDTopic();
        KTDEventManager::getSingleton()->handleMessage(KTDMessageHandler::$onDelete, array("player"=>$ktd_message->getKTDPlayer()));
        $ktd_message->delete();
        $this->getUser()->setFlash('notice', sprintf("Votre message sur le sujet %s a été supprimé avec succés.", $ktd_topic->getTitle()));
        $this->redirect($this->generateUrl('show_topic', $ktd_topic));
    }

  }

  protected function processForm(sfWebRequest $request, sfForm $form, $isNew = false)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
        if($isNew){
            //On envoit l'évenement
            KTDEventManager::getSingleton()->handleMessage(KTDMessageHandler::$onPost,
                                                  array("player" => $this->getUser()->getGuardUser(),
                                                        "amount" => sfConfig::get("app_amount_on_message_post")));



        }
      $ktd_message = $form->save();
      $this->redirect($this->generateUrl('show_topic', $ktd_message->getKTDTopic()).'?page='.$ktd_message->getPage().'#'.$ktd_message->getId());
    }
  }
}
